<template>
  <div>
    <!-- {{store.getters['specs/getSpecs']}} -->
    <el-table :data="store.getters['specs/getSpecs']" border style="width: 100%">
      <el-table-column prop="id" label="规格编号" width="180" />
      <el-table-column prop="specsname" label="规格名称" width="180" />
      <el-table-column prop="address" label="规格属性" width="180">
        <template v-slot="item">
          <div>
            <p v-for="item in item.row.attrs" :key="item">{{ item }}</p>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="item">
          <div>
            {{ item.row.status == 1 ? "启用" : "禁用" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="item">
          <div>
            <el-button type="primary" size="small" @click="edit(item.row.id)"
              >编辑</el-button
            >
            <el-button type="danger" size="small" @click="del(item.row.id)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @current-change="changePage" background layout="prev, pager, next" :total="store.getters['specs/getCount']" :page-size="store.getters['specs/getSize']"/>
  </div>
</template>

<script lang="ts" setup>
import { defineEmits } from "vue";
import { useStore } from "vuex";
import {specsdelete} from "../../request/api";
import { ElMessageBox, ElMessage } from "element-plus";
let store = useStore();

// 编辑传的参
let emit = defineEmits<{
  (e: "edit", value: number): void;
}>();
// 封装一个编辑事件
let edit = (id: number) => {
  emit("edit", id);
};

// 调用仓库
let getSpecs = () => {
  store.dispatch("specs/getSpecsActions");
  store.dispatch("specs/getCountActions");
};
getSpecs();
const count = () => {
    store.dispatch('specs/getCountActions')
}
count()
// 点击拿到当前页码
const changePage = (e:number) => {
    store.dispatch('specs/changePageActions',e)
}

// console.log(store);
const del = (id) => {
  ElMessageBox.confirm("你确定要离开我么！！！", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      specsdelete({ id }).then((res) => {
        // console.log(res)
        ElMessage({
          type: "success",
          message: res.msg,
        });
        getSpecs();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除",
      });
    });
};
</script>

<style lang="less" scoped></style>
